package com.snow.storeapi.controller;

import com.snow.storeapi.entity.Diagnosis;
import com.snow.storeapi.service.IDiagnosisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.annotation.RequestScope;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/diagnosis")
public class DiagnosisController {

    @Autowired
    private IDiagnosisService diagnosisService;

    @GetMapping("/generate/{recordId}/{templateId}")
    public List<Diagnosis> generate(@PathVariable String recordId,
                                    @PathVariable String templateId,
                                    @RequestHeader(value = "Authorization") String token) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        headers.add("User-Agent", "Spring's RestTemplate" );  // value can be whatever
        headers.add("Authorization", token);

        ResponseEntity<Map> response = restTemplate.exchange(
                "http://localhost:3000/recordApi/records/now/" + recordId + "/" + templateId,
                HttpMethod.GET,
                new HttpEntity<>("parameters", headers),
                Map.class
        );
        List<Diagnosis> diagnoses = diagnosisService.generate(response.getBody());
        return diagnoses;
    }
    @GetMapping("/similarity")
    public List<String> findSimilarity(@RequestParam(value = "startRange", defaultValue = "0.7") String startRange,
                                       @RequestParam(value = "endRange", defaultValue = "1") String endRange,
                                       @RequestParam("diseaseCode") String diseaseCode,
                                       @RequestParam("resultId") String resultId
    ) {
        return diagnosisService.findSimilarity(startRange, endRange, diseaseCode, resultId);
    }
}
